package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.InformationBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.NickNameBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 21.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 */
public class NicknameActivity extends Activity {
    private static final String TAG = "NicknameActivity";
    @Bind(R.id.activity_nickname_iv_return)
    ImageView activityNicknameIvReturn;
    @Bind(R.id.activity_nickname_iv_complete)
    TextView activityNicknameIvComplete;
    @Bind(R.id.activity_nickname_et_input)
    EditText activityNicknameEtInput;
    @Bind(R.id.activity_nickname_iv_delet)
    ImageView activityNicknameIvDelet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nickname);
        ButterKnife.bind(this);

        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);
    }

    @OnClick({R.id.activity_nickname_iv_return, R.id.activity_nickname_iv_complete, R.id.activity_nickname_iv_delet})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_nickname_iv_return:
                Intent intent =  new Intent();
                intent.putExtra("cus_name","");
                setResult(5,intent);
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;
            case R.id.activity_nickname_iv_complete:
                String input = activityNicknameEtInput.getText().toString().toString().trim();
                if(!TextUtils.isEmpty(input)){
                    complete(input);
                }else{
                    Toast.makeText(this,"请输入昵称",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.activity_nickname_iv_delet:
                activityNicknameEtInput.setText("");
                break;
        }
    }
    /**
     * @desc 提交昵称
     * @param input*/
    private void complete(final String input) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id", "");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.CHANGE_NICHENG)
                .addParams("cus_id", user_id)
                .addParams("cus_name", input)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e(TAG,"stringparseNetworkResponse----setPicToView---9------->"+stringparseNetworkResponse);
                      final  NickNameBean nickNameBean =  gson.fromJson(stringparseNetworkResponse,NickNameBean.class);
                        if(nickNameBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(NicknameActivity.this,"昵称更改成功",Toast.LENGTH_SHORT).show();
                                   Intent intent =  new Intent();
                                    intent.putExtra("cus_name",input);
                                    setResult(5,intent);
                                    SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("cus_name",input);
                                    edit.commit();
                                    EventBus.getDefault().post(new InformationBean("NicknameActivity",input));
                                    finish();
                                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(NicknameActivity.this,nickNameBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
//        Intent intent =  new Intent();
//        intent.putExtra("cus_name","");
//        setResult(5,intent);
//        finish();
//        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
